package cn.itcast.nems.base.web.controller.b;

import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.nems.base.dao.entity.ResponsibilityCampus;
import cn.itcast.nems.base.service.ResponsibilityCampusDetailService;
import cn.itcast.nems.base.service.ResponsibilityCampusService;
import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.studentrecord.enumeration.StudentRecordErrorBodyEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 员工归属负责校区;注：通常为出纳负责 前端控制器
 * </p>
 *
 * @author czq
 * @since 2024-02-28
 */
@Controller
@RequiredArgsConstructor
@RequestMapping("/b/responsibility/campus")
public class ResponsibilityCampusController {
    @Autowired
    private ResponsibilityCampusService responsibilityCampusService;
    @Autowired
    private ResponsibilityCampusDetailService responsibilityCampusDetailService;

    @GetMapping
    public ResponseEntity<?> list(@RequestParam(value = "page_number", required = false, defaultValue = "0") Integer pageNumber, @RequestParam(value = "page_size", required = false, defaultValue = "30") Integer pageSize,
                                  @RequestParam(value = "employee_email", required = false) String employeeEmail,
                                  @RequestParam(value = "employee_campuss", required = false) List<String> employeeCampuss,
                                  @RequestParam(value = "default_campuss", required = false) List<String> defaultCampuss) {
        // TODO 查询条件
        ResponsibilityCampus myEntity = new ResponsibilityCampus();
        myEntity.setEmployeeEmail(employeeEmail);
        myEntity.setEmployeeCampuss(employeeCampuss);
        myEntity.setDefaultCampuss(defaultCampuss);
        return ResponseEntity.ok().body(responsibilityCampusService.listPage(pageNumber, pageSize, myEntity));
    }

    /**
     * 负责校区下拉
     *
     * @return
     */
    @GetMapping(value = "/employee/campus")
    public ResponseEntity<?> employeeCampusList() {
        ResponsibilityCampus responsibilityCampuses = responsibilityCampusService.getResponsibilityCampusIdsByEmployeeEmail(AccountHolder.getRequiredAccount());
        cn.hutool.core.lang.Assert.notNull(responsibilityCampuses, BizExceptionProducer.produce(StudentRecordErrorBodyEnum.BAD_REQUEST, "暂未配置校区归属信息，请联系系统管理员。"));
        return ResponseEntity.ok().body(responsibilityCampusDetailService.employeeCampusList(responsibilityCampuses.getId(), responsibilityCampuses.getDefaultCampusId()));
    }

    /**
     * 默认校区下拉
     *
     * @return
     */
    @GetMapping(value = "/default/campus")
    public ResponseEntity<?> defaultCampusList(String defaultCampusName) {
        return ResponseEntity.ok().body(responsibilityCampusService.defaultCampusList(defaultCampusName));
    }

    @GetMapping(value = "/{id}")
    public ResponseEntity<ResponsibilityCampus> getById(@PathVariable("id") String id) {
        return ResponseEntity.ok().body(responsibilityCampusService.getById(id));
    }

    @PostMapping
    public ResponseEntity<String> create(@RequestBody ResponsibilityCampus params) {
        return ResponseEntity.ok().body(responsibilityCampusService.create(params));
    }

    @DeleteMapping(value = "/{id}")
    public ResponseEntity<Boolean> delete(@PathVariable("id") String id, @RequestParam(value = "state") String state) {
        return ResponseEntity.ok().body(responsibilityCampusService.delete(id, state));
    }

    @PutMapping
    public ResponseEntity<Boolean> update(@RequestBody ResponsibilityCampus params) {
        return ResponseEntity.ok().body(responsibilityCampusService.update(params));
    }
}
